HAL FLASH initialization and configuration functions migration ¶
Note
In HAL1, the FLASH driver manages Flash memory operations.
In HAL2, depending on the device family, memory operations are managed either by the FLASH driver or by the NVM driver.
For NVM-based families, the NVM driver provides programming services similar to those of FLASH with minor differences:
An explicit erase step is generally not part of the normal NVM write flow.
Erase-by-address, page erase, bank erase, and FLASH mass erase remain FLASH-specific.
For NVM-based families, the only erase service that may be exposed is OBK erase, when that feature exists.
All HAL2 FLASH initialization and configuration are applicable also for NVM. Simply replace FLASH with NVM to get the corresponding NVM initialization and configuration
The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:
Open STM32CubeMX2 FLASH configuration panel.
Configure the SW layer HAL.
Configure the FLASH main features.
Configure the advanced FLASH features if needed.
Configure the NVIC if the application requires the FLASH to be used in IT mode.
Generate the code.
HAL FLASH configuration panel ¶
Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.
HAL FLASH main features configuration ¶
Configure the FLASH main features.
HAL FLASH advanced features configuration ¶
Configure the advanced FLASH features if needed.
NVIC configuration ¶
Configure the NVIC in case of an interrupt.
Generated FLASH initialization and de-initialization sequences ¶
The FLASH initialization sequence is generated in the
mx_flash_init
and
mx_flash_deinit
functions in the
mx_flash.c
file under
$YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.
Related concepts:
Important
In STM32CubeMX, some option byte (OB) features are handled directly within the initialization code. In contrast, STM32CubeMX2 primarily manages the IO processes such as programming, ECC handling, and programming modes.
|
Topic |
Code Snippet HAL1 |
Code Snippet HAL2 |
|---|---|---|
|
Handle declaration |
/* No handle declaration for FLASH in HAL1 */
|
static hal_flash_handle_t hFLASH;
|
|
Initialization and configuration sequence |
static void MX_FLASH_Init(void)
{
if (HAL_FLASH_Unlock() != HAL_OK)
{
Error_Handler();
}
/*
* This section is reserved for configuring the selected option byte (OB) features generated by STM32CubeMX.
* It may include unlocking the OB, setting OB parameters, and programming OBs as required.
*/
if (HAL_FLASH_Lock() != HAL_OK)
{
Error_Handler();
}
}
|
hal_flash_handle_t *mx_flash_init(void)
{
if (HAL_FLASH_Init(&hFLASH, HAL_FLASH) != HAL_OK)
{
return NULL;
}
if (HAL_FLASH_SetProgrammingMode(&hFLASH, HAL_FLASH_PROGRAM_QUADWORD) != HAL_OK)
{
return NULL;
}
HAL_FLASH_ITF_ECC_EnableIT(HAL_FLASH);
/* Enable the interruption for FLASH */
HAL_CORTEX_NVIC_SetPriority(FLASH_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
HAL_CORTEX_NVIC_EnableIRQ(FLASH_IRQn);
return &hFLASH;
}
|
|
Deinitialization sequence |
Note FLASH in HAL1 has no handle and does not treat the IO process in STM32CubeMX. therefore, there is no DeInit function |
void mx_flash_deinit(void)
{
HAL_FLASH_DeInit(&hFLASH);
/* Disable the interruption for FLASH */
HAL_CORTEX_NVIC_DisableIRQ(FLASH_IRQn);
}
hal_flash_handle_t *mx_flash_gethandle(void)
{
return &hFLASH;
}
|
|
IRQ handle function |
void FLASH_IRQHandler(void)
{
HAL_FLASH_IRQHandler();
}
|
void FLASH_IRQHandler(void)
{
HAL_FLASH_IRQHandler(&hFLASH);
}
|